﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using Microsoft.Win32;

namespace org.googlecoders.GetLatestChromium
{
	public class UserSettings : ApplicationSettingsBase
	{
		#region Program Settings
		[UserScopedSetting]
		[DefaultSettingValue("false")]
		public bool StartWithWindows
		{
			get { return (bool)this["StartWithWindows"]; }
			set {this["StartWithWindows"] = value; }
		}

		[UserScopedSetting]
		[DefaultSettingValue("false")]
		public bool CheckOnStart
		{
			get { return (bool)this["CheckOnStart"]; }
			set { this["CheckOnStart"] = value; }
		}

		[UserScopedSetting]
		[DefaultSettingValue("false")]
		public bool QuitOnUpdate
		{
			get { return (bool)this["QuitOnUpdate"]; }
			set { this["QuitOnUpdate"] = value; }
		}
		#endregion Program Settings

		#region Update Settings
		[UserScopedSetting]
		[DefaultSettingValue("")]
		public string Source
		{
			get { return (string)this["Source"]; }
			set { this["Source"] = value; }
		}

		[UserScopedSetting]
		[DefaultSettingValue("1")]
		public int Interval
		{
			get { return (int)this["Interval"]; }
			set { this["Interval"] = value; }
		}

		[UserScopedSetting]
		[DefaultSettingValue("01/01/0001")]
		public DateTime LastCheck
		{
			get { return (DateTime)this["LastCheck"]; }
			set { this["LastCheck"] = value; }
		}

		[UserScopedSetting]
		[DefaultSettingValue("01/01/0001")]
		public DateTime NextCheck
		{
			get { return (DateTime)this["NextCheck"]; }
			set { this["NextCheck"] = value; }
		}
		#endregion Update Settings

		#region Version Numbers
		[UserScopedSetting]
		[DefaultSettingValue("0")]
		public int OurRevision
		{
			get { return (int)this["OurRevision"]; }
			set { this["OurRevision"] = value; }
		}

		[UserScopedSetting]
		[DefaultSettingValue("0")]
		public int TheirRevision
		{
			get { return (int)this["TheirRevision"]; }
			set { this["TheirRevision"] = value; }
		}

		[UserScopedSetting]
		[DefaultSettingValue("0")]
		public int OurBuild
		{
			get { return (int)this["OurBuild"]; }
			set { this["OurBuild"] = value; }
		}

		[UserScopedSetting]
		[DefaultSettingValue("0")]
		public int TheirBuild
		{
			get { return (int)this["TheirBuild"]; }
			set { this["TheirBuild"] = value; }
		}
		#endregion Version Numbers

		[UserScopedSetting]
		[DefaultSettingValue("")]
		public string InstallerPath
		{
			get { return (string)this["InstallerPath"]; }
			set { this["InstallerPath"] = value; }
		}

		public override void Save()
		{
			base.Save();
			
			try
			{
				if(StartWithWindows)
				{
					RegistryKey run = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run",true);
					if(run != null)
					{
						run.DeleteValue("GetLatestChromium");
						run.Close();
					}
				}
				else
				{
					RegistryKey run = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run",true);
					if(run != null)
					{
						run.SetValue("GetLatestChromium",System.Windows.Forms.Application.ExecutablePath,RegistryValueKind.String);
						run.Close();
					}
				}
			}
			catch(Exception ex)
			{
				
			}	
		}
	}
}
